//
//  TicketPersonViewController.m
//  SXH
//
//  Created by Jack on 15/12/10.
//  Copyright © 2015年 lyss. All rights reserved.
//

#import "TicketPersonViewController.h"
#import "TicketPersonTableViewCell.h"
#import "TicketPersonEntity.h"
#import "AddTicketPersonViewController.h"
#import "CommonService.h"

@interface TicketPersonViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableViewTicketPerson;
@property (strong, nonatomic) NSMutableArray *tableArrayTicketPerson;

@end

@implementation TicketPersonViewController
@synthesize tableViewTicketPerson;
@synthesize tableArrayTicketPerson;
@synthesize orderEnsureViewController,userEntity;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    tableArrayTicketPerson = [[NSMutableArray alloc]init];
    [self getTicketsList];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(IBAction)addTicketPerson:(id)sender{
    AddTicketPersonViewController *vc = [[AddTicketPersonViewController alloc]init];
    vc.ticketPersonView = self;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [tableArrayTicketPerson count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *couponTableViewCellIdentifier=@"TicketPersonTableViewCell";
    TicketPersonTableViewCell *cell = (TicketPersonTableViewCell *)[tableView dequeueReusableCellWithIdentifier:couponTableViewCellIdentifier];
    if (cell == nil) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"TicketPersonTableViewCell" owner:self options:nil];
        cell = [array objectAtIndex:0];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    TicketPersonEntity *ticketPersonEntity = [tableArrayTicketPerson objectAtIndex:indexPath.row];
    cell.name.text = ticketPersonEntity.name;
    cell.phone.text = ticketPersonEntity.phone;
    cell.btnSelectTicketPerson.tag = indexPath.row;
    cell.btnEditTicketPerson.tag = indexPath.row;
    [cell.btnSelectTicketPerson addTarget:self action:@selector(selectTicketPerson:) forControlEvents:UIControlEventTouchUpInside];
    [cell.btnEditTicketPerson addTarget:self action:@selector(editTicketPerson:) forControlEvents:UIControlEventTouchUpInside];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 45;
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
-(void)selectTicketPerson:(UIButton *)btn{
    TicketPersonEntity *ticketPersonEntity = [tableArrayTicketPerson objectAtIndex:btn.tag];
    orderEnsureViewController.ticketPersonEntity = ticketPersonEntity;
    orderEnsureViewController.ticketPerson.text = [NSString stringWithFormat:@"%@    %@",ticketPersonEntity.name,ticketPersonEntity.phone];
    [self.navigationController popViewControllerAnimated:YES];

}
-(void)editTicketPerson:(UIButton *)btn{
    TicketPersonEntity *ticketPersonEntity = [tableArrayTicketPerson objectAtIndex:btn.tag];
    AddTicketPersonViewController *vc = [[AddTicketPersonViewController alloc]init];
    vc.ticketPersonEntity = ticketPersonEntity;
    vc.ID = [NSString stringWithFormat:@"%ld", (long)btn.tag];
    vc.ticketPersonView = self;
    [[self navigationController]pushViewController:vc animated:YES];
}

- (void) getTicketsList {
    CommonService *service = [[CommonService alloc] init];
    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:
                           userEntity.userId, @"user_id",nil];
    
    [service getNetWorkData:param
                       Path:TICKET_PERSON_GETBYUSERID
                  Successed:^(id entity) {
                      NSDictionary *dic = (NSDictionary *)entity;
                      
                      NSArray *array = [dic objectForKey:@"rows"];
                      for (NSDictionary* attributes in array) {
                          TicketPersonEntity *entity = [[TicketPersonEntity alloc] init];
                          entity = [entity initWithAttributes:attributes];
                          [tableArrayTicketPerson addObject:entity];
                      }
                      dispatch_async(dispatch_get_main_queue(), ^{
                          [tableViewTicketPerson reloadData];
                      });
                  } Failed:^(NSInteger errorCode, NSString *message) {
                      [tableViewTicketPerson reloadData];
                  }];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"TicketPersonViewController"];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"TicketPersonViewController"];
}

@end
